﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace WorldOnFire
{
    public class CCarrier : ABCEnemy
    {
        private float[] m_fBomberTimer;
        private int[] m_nBomberCount;
        private List<int>[] m_BomberIndexList;
        private bool[] m_bBombersReleased;

        public override void Initialize(int _nObjectCount)
        {
            m_fBomberTimer = new float[_nObjectCount];
            m_nBomberCount = new int[_nObjectCount];
            m_BomberIndexList = new List<int>[_nObjectCount];
            m_bBombersReleased = new bool[_nObjectCount];

            base.Initialize(_nObjectCount);

            // Initialize the members
            m_fOrbitAngleLock = 0.1f;

            for (int iObject = 0; iObject < _nObjectCount; ++iObject)
            {
                m_BomberIndexList[iObject] = new List<int>();

                m_fMass = 500.0f;
                m_fEngineForce = 10.0f * m_fMass;
                m_fMaximumSpeed = 10.0f;
                m_fTurnSpeed = 0.1f * (float)Math.PI;

                m_fMinimumRange = 500.0f;
                m_fMaximumRange = 500.0f;
                m_fShotDelayPrimary = 0.0f;
                m_fShotDelaySecondary = 0.0f;
                m_fShotSpeed = 0.0f;
                m_fDamage = 0.0f;
                m_fAreaOfEffect = 0.0f;
                m_fMaximumHealth = 400.0f;

                // TODO: SET TARGET INDEX
                m_nTargetIndex[iObject] = 0;
                m_fDisabledTimer[iObject] = 0.0f;
                m_fShotTimerPrimary[iObject] = 0.0f;

                // TODO: LOLWUT
                this.SetAIState(iObject, eAIState.CLOSING);
            }
        }

        public override void LoadContent()
        {
            m_eModelID = CRenderingManager.eModelID.ENEMY_CARRIER;

            base.LoadContent();
        }

        public override void Update(float _fDeltaTime)
        {
            for (int iObject = 0; iObject < m_nObjectCount; ++iObject)
            {
                // Store the bomber class
                CBomber Bomber = (CBomber)s_ObjectManager.GetEnemy(ABCEnemy.eEnemyType.BOMBER);
                List<int> BomberIndexList = m_BomberIndexList[iObject];

                // Loop through each contained bomber
                for (int iBomber = BomberIndexList.Count - 1; iBomber >= 0; --iBomber)
                {
                    // If the bomber is no longer alive
                    if (Bomber.IsObjectActive(BomberIndexList[iBomber]) == false)
                    {
                        // Remove the bomber from the index list
                        BomberIndexList.RemoveAt(iBomber);

                        // Increment the number of bombers needing creation
                        ++m_nBomberCount[iObject];

                        // Increment the delay until another fighter is spawned
                        m_fBomberTimer[iObject] += 5.0f;
                    }
                }

                // Decrement the bomber timer
                m_fBomberTimer[iObject] -= _fDeltaTime;

                // If a bomber is ready to launch
                if (m_nBomberCount[iObject] > 0 && m_fBomberTimer[iObject] < 0.0f 
                    && Bomber.GetActiveObjectCount() != Bomber.GetObjectCount())
                {
                    // Create a bomber
                    m_BomberIndexList[iObject].Add(Bomber.CreateEnemy(this, iObject));
                    Bomber.ScheduleForActivation(BomberIndexList[BomberIndexList.Count - 1]);
                    Bomber.ApplyForce(BomberIndexList[BomberIndexList.Count - 1],
                        100.0f * Bomber.GetMass() * Vector3.Down);

                    // Reset the bomber timer and decrement the number of bombers remaining
                    m_fBomberTimer[iObject] = 1.5f;
                    --m_nBomberCount[iObject];
                }
            }

                base.Update(_fDeltaTime);
        }

        protected override void FirePrimary(int _nIndex)
        {
            if (m_bBombersReleased[_nIndex] == false)
            {
                m_nBomberCount[_nIndex] = 5;
                m_bBombersReleased[_nIndex] = true;
            }
        }
    }
}
